import type { App } from "vue";
import { createRouter, createWebHashHistory, type RouteRecordRaw } from "vue-router";

export const Layout = () => import("@/layout/index.vue");

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/",
    name: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        // 用于 keep-alive 功能，需要与 SFC 中自动推导或显式声明的组件名称一致
        // 参考文档: https://cn.vuejs.org/guide/built-ins/keep-alive.html#include-exclude
        name: "Dashboard",
        meta: {
          title: "dashboard",
          icon: "homepage",
          affix: true,
          keepAlive: true,
        },
      },
      {
        path: "401",
        component: () => import("@/views/error/401.vue"),
        meta: { hidden: true },
      },
      {
        path: "404",
        component: () => import("@/views/error/404.vue"),
        meta: { hidden: true },
      },
      {
        path: "profile",
        name: "Profile",
        component: () => import("@/views/profile/index.vue"),
        meta: { title: "个人中心", icon: "user", hidden: true },
      },
      {
        path: "myNotice",
        name: "MyNotice",
        component: () => import("@/views/system/notice/components/MyNotice.vue"),
        meta: { title: "我的通知", icon: "user", hidden: true },
      },
    ],
  },
  {
    path: "/system",
    name: "System",
    component: Layout,
    redirect: "/system/user",
    meta: { hidden: false, title: "系统管理" },
    children: [
      {
        path: "user",
        name: "User",
        component: () => import("@/views/system/user/index.vue"),
        meta: { hidden: false, title: "用户管理", icon: "el-icon-User", keepAlive: true },
      },
      {
        path: "role",
        name: "Role",
        component: () => import("@/views/system/role/index.vue"),
        meta: { hidden: false, title: "角色管理", icon: "role", keepAlive: true },
      },
      {
        path: "menu",
        name: "SysMenu",
        component: () => import("@/views/system/menu/index.vue"),
        meta: { hidden: false, title: "菜单管理", icon: "menu", keepAlive: true },
      },
      {
        path: "dept",
        name: "SysDept",
        component: () => import("@/views/system/dept/index.vue"),
        meta: { hidden: false, title: "部门管理", icon: "menu", keepAlive: true },
      },
    ],
  },
  {
    path: "/dataSourceManager",
    name: "DataSourceManager",
    component: Layout,
    meta: { hidden: false, title: "数据源管理" },
    children: [
      {
        path: "dataSourceManage",
        name: "DataSourceManage",
        component: () => import("@/views/dataSourceManage/index.vue"),
        meta: { hidden: false, title: "数据源管理", icon: "setting", keepAlive: true },
      },
    ],
  },
  {
    path: "/indicationManager",
    name: "IndicationManager",
    component: Layout,
    meta: { hidden: false, title: "指标管理" },
    children: [
      {
        path: "indicationManage",
        name: "IndicationManage",
        component: () => import("@/views/indicationManage/index.vue"),
        meta: { hidden: false, title: "指标管理", icon: "setting", keepAlive: true },
      },
      {
        path: "insertDatabase",
        name: "InsertDatabase",
        component: () => import("@/views/strategy/strategy-index/insert-database/index.vue"),
        meta: { hidden: true, title: "根据数据库新建指标", icon: "setting", keepAlive: true },
      },
      {
        path: "insertIndicator",
        name: "InsertIndicator",
        component: () => import("@/views/strategy/strategy-index/insert-indicator/index.vue"),
        meta: { hidden: true, title: "根据指标新建指标", icon: "setting", keepAlive: true },
      },
    ],
  },
  {
    path: "/indicatorSolidificationManage",
    name: "IndicatorSolidificationManage",
    component: Layout,
    meta: { hidden: false, title: "指标固化" },
    children: [
      {
        path: "indicatorSolidification",
        name: "IndicatorSolidification",
        component: () => import("@/views/indicatorSolidification/index.vue"),
        meta: { hidden: false, title: "指标固化", icon: "el-icon-coin", keepAlive: true },
      },
    ],
  },
  // {
  //   path: "/strategy",
  //   name: "strategy",
  //   component: () => import("@/views/strategy/strategy-index/insert-database/index.vue"),
  //   meta: { hidden: true, title: "新建指标工作流" },
  // },
  // {
  //   path: "/insertIndicator",
  //   name: "insertIndicator",
  //   component: () => import("@/views/strategy/strategy-index/insert-indicator/index.vue"),
  //   meta: { hidden: true, title: "新建指标工作流" },
  // },
  // {
  //   path: "/strategy/indicatorOutput",
  //   name: "indicatorOutput",
  //   component: () => import("@/views/strategy/strategy-index/insert-database/indicatorOutput.vue"),
  //   meta: { hidden: true },
  // },
  // {
  //   path: "/strategy/createIndicator",
  //   name: "createIndicator",
  //   component: () => import("@/views/strategy/strategy-index/insert-database/createIndicator.vue"),
  //   meta: { hidden: true },
  // },
  {
    path: "/strategyLog",
    name: "StrategyLog",
    component: Layout,
    meta: { hidden: false, title: "策略日志" },
    children: [
      {
        path: "strategyLog",
        name: "strategyLog",
        component: () => import("@/views/strategy/log/index.vue"),
        meta: { hidden: false, title: "策略日志", icon: "setting", keepAlive: true },
      },
    ],
  },
  {
    path: "/redirect",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
    ],
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: { hidden: true },
  },
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

// 全局注册 router
export function setupRouter(app: App<Element>) {
  app.use(router);
}

export default router;
